﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using CustomControls.Dialog;
using CustomControls.MiscControls;
using CustomControls.SearchResult;
using Infrasturcture.Global.Helpers.CustomHelpers.Events;
using Infrasturcture.TouchLibrary;
using MallInteractiveMap.ClientDataHandler;
using MallInteractiveMap.DTO;
using MallInteractiveMap.Language;
using TouchControls;
using WpfKb.Controls;
using TouchAction = TouchFramework.TouchAction;

namespace MallInteractiveMap
{
    public partial class MainWindow
    {
        private void RegisterSearchBox()
        {
            SearchBox.GotFocus += SearchBoxGotFocus;
            SearchBox.PreviewKeyDown += SearchBox_PreviewKeyDown;
            FrameworkManager.RegisterElement(SearchBox, false, new[] { TouchAction.Tap });
        }

        private void SearchBoxGotFocus(object sender, RoutedEventArgs e)
        {
            _keyboard = new FloatingTouchScreenKeyboard
            {
                Name = "Keyboard",
                Width = 900,
                Height = 400,
                AreAnimationsEnabled = true
            };
            FrameworkManager.RegisterElement((IMTouchControl)_keyboard, true, null);
            MapHolder.Children.Add(_keyboard);
        }

        void SearchBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                HideKeyBoard();
                if (SearchBox.Text != string.Empty)
                    Search(SearchBox.Text);
            }
        }

        private void HideKeyBoard()
        {
            if (_keyboard == null) return;

            FrameworkManager.UnRegisterElement(_keyboard);
            MapHolder.Children.Remove(_keyboard);
            _keyboard = null;
            HideFocus();
        }

        private void HideFocus()
        {
            var parent = (FrameworkElement)SearchBox.Parent;
            while (parent != null && parent is IInputElement && !((IInputElement)parent).Focusable)
            {
                parent = (FrameworkElement)parent.Parent;
            }
            var scope = FocusManager.GetFocusScope(SearchBox);
            FocusManager.SetFocusedElement(scope, parent);
        }

        private void Search(string query)
        {
            WaitScreen.Show();
            List<DTOShop> result = null;
            try
            {
                result = ClientData.SearchShops(query);
                if (result.Count == 0)
                {
                    WaitScreen.Hide();
                    ShowMessage("", LanguageLoader.GetText("There search returned zero results."));
                    return;
                }

                var shopSearchResultViewer = new ShopSearchResult(result);
                ShowDialog(shopSearchResultViewer);
                foreach (var shopResultItem in shopSearchResultViewer.ShopResultItems)
                {
                    shopResultItem.ShopButton.Tag = FrameworkManager.RegisterElement(shopResultItem.ShopButton, false, new[] { TouchAction.Tap });
                }

                shopSearchResultViewer.ScrollViewer.Tag = FrameworkManager.RegisterElement(shopSearchResultViewer.ScrollViewer, false, new[] { TouchAction.Slide });
                shopSearchResultViewer.Closed += OnSearchClosed;
            }
            finally
            {
                WaitScreen.Hide();
            }
        }

        private void OnSearchClosed(object sender, DataEventArgs e)
        {
            var shopSearchResultViewer = sender as ShopSearchResult;
            foreach (var item in shopSearchResultViewer.ShopResultItems)
            {
                FrameworkManager.UnRegisterElement(((IMTContainer)item.ShopButton.Tag).Id);
            }
            FrameworkManager.UnRegisterElement(((IMTContainer)shopSearchResultViewer.ScrollViewer.Tag).Id);
            var shopResultItem = e.Data as ShopResultItem;
            if (shopResultItem != null)
            {
                ShowShopInformation(GetShop(shopResultItem.ShopNumber), null);
            }

        }


    }
}
